﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using Leadtools.Codecs;


namespace ImageCom.LeadtoolEx
{
    public class ImageFileEx : LDEx
    {
        public static void ProcessCombineSmallPic(string inPath, SizeF inSizeF,bool deleteSrcFile=true,bool searchChildFolder=false)
        {
            List<FileSizeInfo> listFile = new List<FileSizeInfo>();
            LDEx.GetShortImageInfo(inPath, listFile, searchChildFolder);
            if (listFile.Count > 1)
            {
                var q = from ph in listFile
                        where
                            ph.Width <= (decimal)inSizeF.Width && ph.Heigth <= (decimal)inSizeF.Height
                        select ph;
                listFile = q.ToList();
                if (listFile.Count < 1)
                {
                    return;
                }
                List<int> listGroup = GetCombo(listFile.Count);
                int twoCopule = listGroup[0];
                int threeCopule = listGroup[1];
                int fourCopule = listGroup[2];
                listFile = listFile.OrderBy(o => (o.Width)).ToList();
                ///处理2张
                int current = 0;
            
                if (twoCopule != 0)
                {
                    for (int i = 0; i < twoCopule; i++)
                    {
                        CombinTwoImageFromFile(listFile.Skip(current + i * 2).Take(2).ToList());
                    }

                }
                current = current + twoCopule * 2;
                if (threeCopule != 0)
                {
                    for (int i = 0; i < threeCopule; i++)
                    {
                        CombinThreeImageFromFile(listFile.Skip(current + i * 3).Take(3).ToList());
                    }
                }
                current = current + threeCopule * 3;
                if (fourCopule != 0)
                {
                    for (int i = 0; i < fourCopule; i++)
                    {
                        CombinFourImageFromFile(listFile.Skip(current + i * 4).Take(4).ToList());
                    }
                    //CombinFourImageFromFile(listFile.Skip(current).Take(fourCopule*4).ToList());
                }

            }
        }

        public static void  GetAllContinePhotoFolder(string inFolder,List<string> listPath)
        {
            foreach (string file in Directory.GetFiles(inFolder))
            {
                if (Jpgflag.Contains(Path.GetExtension(file).ToUpper()))
                {
                    listPath.Add(inFolder);
                    break;
                }

            }
            foreach (string directory in Directory.GetDirectories(inFolder))
            {
                GetAllContinePhotoFolder(directory, listPath);
            }
        }

        public static void CombineFolder(string inpath)
        {
            if (CheckCombine(inpath))                    
            {
                List<string> processFolderList = new List<string>();
                GetAllContinePhotoFolder(inpath, processFolderList);
                foreach (string folder in processFolderList)
                {
                    ProcessCombineSmallPic(folder, new SizeF(3.5f, 5f));
                }
                FlogFolder(inpath);
            }
          
        }

        private static bool CheckCombine(string inpath)
        {
            string txtFile = Path.Combine(inpath, "zzhb.txt");
            return !File.Exists(txtFile);
        }

        private static void FlogFolder(string inpath)
        {
            string txtFile = Path.Combine(inpath, "zzhb.txt");
            if (!File.Exists(txtFile))
            {
                var stream=File.Create(txtFile);
                 stream.Close();
            }
        }

        private static List<int> GetCombo(int inNum)
        {
            int maxTwo = inNum % 2;
            int maxThree = 0;
            if (maxTwo == 1)
            {
                maxTwo = inNum / 2 - 1;
                if (maxTwo >= 0)
                {
                    maxThree = 1;
                }
                else
                {
                    maxThree = 0;

                }
            }
            else
            {
                maxTwo = inNum / 2;
            }
            int maxFour = maxTwo / 2;
            maxTwo = maxTwo % 2;
            return new List<int>() { maxTwo, maxThree, maxFour };
        }

    }
}
